package com.example.easynote;

import photos.core.commons.Contants;
import DTO.AlbumModel;
import Help.Manager.Album.AlbumAdapter;
import Help.Manager.Album.App;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.example.easynote.R;

/*
 * Activity hiá»ƒn thá»‹ ná»™i dung chi tiáº¿t cá»§a má»™t Album.
 */
public class AlbumActivity extends Activity implements OnItemClickListener{

	GridView grid;
	AlbumModel album;
	BaseAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album);
		// Show the Up button in the action bar.
		setupActionBar();
		
		// Láº¥y Album Path.
		String albumPath = getIntent().getStringExtra(Contants.AlbumPath);
		if(albumPath == null)
			return;
		
		// TÃ¬m Album dá»±a vÃ o Path.
		album = App.getAlbumsController().getAlbum(albumPath);
		if(album == null)
			return;
		
		// TiÃªu Ä‘á»� Activity lÃ  tiÃªu Ä‘á»� cá»§a Album.
		this.setTitle(album.getTitle());
		
		grid = (GridView)findViewById(R.id.album_gridview);
		
		this.adapter = new AlbumAdapter(AlbumActivity.this, this.album);
		grid.setAdapter(this.adapter);
		
		grid.setOnItemClickListener(this);
		
		registerForContextMenu(grid);
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.album, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterContextMenuInfo cmi = (AdapterContextMenuInfo) menuInfo;

		int id = v.getId();
		if(id == R.id.album_gridview) {
			menu.add(1, cmi.position, 0, R.string.delete_photo);
		}
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		int id = item.getGroupId();
		if(id == 1) {
			int position = item.getItemId();

			App.getPhotosData().removePhoto(position, this.album, App.getAlbumsController());
			
			this.adapter.notifyDataSetChanged();
		}

		return super.onContextItemSelected(item);
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView, android.view.View, int, long)
	 * Báº¯t sá»± kiá»‡n lá»±a chá»�n trÃªn GridView.
	 * Chuyá»ƒn Ä‘áº¿n Activity FullScreenImageActivity.
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		// TODO Auto-generated method stub
		Intent intent = new Intent(AlbumActivity.this, FullScreenImageActivity.class);
	
		intent.putExtra(Contants.AlbumPath, this.album.getPath());
		intent.putExtra(Contants.Position, position);
		
		startActivity(intent);
	}

}
